package org.example.StudentManagementSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.example.StudentManagementSystem.entity.*;
import org.example.StudentManagementSystem.entity.Class;
import org.example.StudentManagementSystem.common.Result;
import org.example.StudentManagementSystem.dto.response.EnrollmentCountDTO;
import org.example.StudentManagementSystem.service.*;
import org.example.StudentManagementSystem.dto.response.ClassStudentCountDTO;
import org.example.StudentManagementSystem.dto.response.TeacherCourseCountDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.time.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/data")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class DataVisualizationController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassService classService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private EnrollmentService enrollmentService;

    /**
     * 获取各个班级学生人数
     */
    @Operation(summary = "获取各个班级学生人数", description = "返回各个班级的学生人数")
    @ApiResponse(responseCode = "200", description = "成功获取学生人数",
            content = @Content(schema = @Schema(implementation = ClassStudentCountDTO.class)))
    @GetMapping("/student")
    public Result getClassStudentNum() {
        List<Class> classes = classService.list();
        List<ClassStudentCountDTO> classStudentCountDTOS = new ArrayList<>();
        for (Class aClass : classes) {
            ClassStudentCountDTO classStudentCountDTO = new ClassStudentCountDTO();
            classStudentCountDTO.setClassName(aClass.getName());
            QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("class_id", aClass.getId());
            List<Student> students = studentService.list(queryWrapper);
            classStudentCountDTO.setCount(students.size());
            classStudentCountDTOS.add(classStudentCountDTO);
        }
        return Result.success(classStudentCountDTOS, "获取学生人数成功");
    }

    /**
     * 获取各个教师课程数量
     */
    @Operation(summary = "获取各个教师课程数量", description = "返回各个教师的课程数量")
    @ApiResponse(responseCode = "200", description = "成功获取教师课程数量",
            content = @Content(schema = @Schema(implementation = TeacherCourseCountDTO.class)))
    @GetMapping("/teacher")
    public Result getTeacherCourseNum() {
        List<Teacher> teachers = teacherService.list();
        List<TeacherCourseCountDTO> teacherCourseCountDTOS = new ArrayList<>();
        for (Teacher teacher : teachers) {
            TeacherCourseCountDTO teacherCourseCountDTO = new TeacherCourseCountDTO();
            teacherCourseCountDTO.setName(teacher.getName());
            QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_id", teacher.getId());
            List<Course> courses = courseService.list(queryWrapper);
            teacherCourseCountDTO.setCount(courses.size());
            teacherCourseCountDTOS.add(teacherCourseCountDTO);
        }
        return Result.success(teacherCourseCountDTOS, "获取教师课程数量成功");
    }

    /**
     * 获取课程报名数
     */
    @Operation(summary = "获取课程报名数", description = "返回最近三天每天的课程报名数量")
    @ApiResponse(responseCode = "200", description = "成功获取报名数",
            content = @Content(schema = @Schema(implementation = EnrollmentCountDTO.class)))
    @GetMapping("/enrollment")
    public Result getEnrollmentNum() {
        // 获取当前日期
        LocalDate now = LocalDate.now();
        // 计算7天前的日期
        LocalDate sevenDaysAgo = now.minusDays(3);
        List<EnrollmentCountDTO> result = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            LocalDate currentDate = sevenDaysAgo.plusDays(i);
            QueryWrapper<Enrollment> queryWrapper = new QueryWrapper<>();
            queryWrapper.between("enrollment_time", currentDate.atStartOfDay(), currentDate.plusDays(1).atStartOfDay());
            List<Enrollment> enrollments = enrollmentService.list(queryWrapper);
            EnrollmentCountDTO enrollmentCountDTO = new EnrollmentCountDTO();
            enrollmentCountDTO.setCount(enrollments.size());
            enrollmentCountDTO.setDate(Date.valueOf(currentDate));
            result.add(enrollmentCountDTO);
        }
        return Result.success(result, "获取报名数成功");
    }
}
